﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;
using System.Collections;

namespace LookUpLater
{
    public class LookUpLaterDB:IDisposable
    {
        private static LookUpLaterDB currentLookUp;
        private LookUpLaterDB()
        {
            LoadXml();
        }
        public LookUpLaterDB(List<ItemType> items)
        {
            this.Items = items;
        }
        private void LoadXml()
        {
            var xmlPath = Path.Combine(Directory.GetCurrentDirectory(), "LookUpLaterDB.xml");
            var rootNode = XDocument.Load(xmlPath).Element("LookUpLaterDB");
            this.Items = rootNode.Elements("Item").Select(i => new ItemType(i)).ToList();
        }
        public static LookUpLaterDB Refresh()
        {
            currentLookUp = new LookUpLaterDB();
            return currentLookUp;
        }
        public List<ItemType> Items { get; set; }
        public static LookUpLaterDB CurrentLookUp
        {
            get
            {
                if (currentLookUp == null)
                    currentLookUp = new LookUpLaterDB();
                return currentLookUp;
            }
        }
        public void Dispose()
        {
            currentLookUp = null;
        }
        public bool Save()
        {
            try
            {
                
                var xmlPath = Path.Combine(Directory.GetCurrentDirectory(), "LookUpLaterDB.xml");
                var rootDoc = XDocument.Load(xmlPath);
                var rootNode=rootDoc.Element("LookUpLaterDB");
                //If Node exists then add to that node else create new node
                var itemFound = rootNode.Elements("Item").Any()?
                    rootNode.Elements("Item").FirstOrDefault(i => new ItemType(i).Equals(this.Items.First())):null;

                if (itemFound != null)
                    itemFound.Add(this.Items.First().Lookups.First().ToXml());
                else
                    rootNode.Add(this.Items.Select(i => i.ToXml()));

                rootDoc.Save(xmlPath);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool Delete(Lookup lookup)
        {
            try
            {
                var xmlPath = Path.Combine(Directory.GetCurrentDirectory(), "LookUpLaterDB.xml");
                var rootDoc = XDocument.Load(xmlPath);
                var rootNode = rootDoc.Element("LookUpLaterDB");
                //If Node exists then add to that node else create new node
                var itemFound = rootNode.Elements("Item").Any() ?
                    rootNode.Elements("Item").First(i => new ItemType(i).Equals(lookup)) : null;

                if (itemFound != null)
                    itemFound.Add(this.Items.First().Lookups.First().ToXml());
                else
                    rootNode.Add(this.Items.Select(i => i.ToXml()));

                rootDoc.Save(xmlPath);
                currentLookUp = Refresh();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
